package com.yisu.expressway.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.yisu.expressway.R;
import com.yisu.expressway.activity.ScenicSpotDetailActivity;
import com.yisu.expressway.model.ProductItem;
import com.yisu.expressway.model.ServiceDistrictObj;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/14 0014.
 */
public class ScenicSpotListAdapter extends  RecyclerView.Adapter<ScenicSpotListAdapter.ScenicSpotViewHolder>  {

    ArrayList<ServiceDistrictObj.ViewSpotItemObj> mData = new ArrayList<>();
    private Context mContext;
    private int mServiceAreaId;

    public ScenicSpotListAdapter (Context context,int serviceId){
        this.mContext = context;
        mServiceAreaId = serviceId;
    }

    public void setData(ArrayList<ServiceDistrictObj.ViewSpotItemObj> list){
        this.mData = list;
        notifyDataSetChanged();
    }

    @Override
    public ScenicSpotViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.view_spot_item, parent, false);
        return new ScenicSpotViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(ScenicSpotViewHolder holder, int position) {
        if(mData.size() <= position ){
            return;
        }
        final ServiceDistrictObj.ViewSpotItemObj item = mData.get(position);

        Glide.with(mContext)
                .load(item.imageUrl)
                .placeholder(R.drawable.blank)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .into(holder.mIv_img);

        holder.mTv_des.setText(item.subTitle);
        holder.mTv_name.setText(item.mainTitle);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ScenicSpotDetailActivity.launch(mContext,mServiceAreaId,item.id);
            }
        });


    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class ScenicSpotViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.iv_view_img) ImageView mIv_img;
        @Bind(R.id.tv_view_name) TextView mTv_name;
        @Bind(R.id.tv_view_des) TextView mTv_des;

        public ScenicSpotViewHolder(View itemView){
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
